package com.wyix;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author wangdx
 * @since 2025/8/20
 */
@SpringBootApplication
public class App implements CommandLineRunner {
    private final DataSource dataSource;
    private final JdbcTemplate jdbcTemplate;

    public App(DataSource dataSource, JdbcTemplate jdbcTemplate) {
        this.dataSource = dataSource;
        this.jdbcTemplate = jdbcTemplate;
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // 打印当前使用的数据源类型
        System.out.println("=====================================");
        System.out.println("使用的数据源: " + dataSource.getClass().getName());

        // 插入测试数据
        jdbcTemplate.update("INSERT INTO \"user\" (name, email, age) VALUES (?, ?, ?)",
                "张三", "zhangsan@example.com", 18);
        jdbcTemplate.update("INSERT INTO \"user\" (name, email, age) VALUES (?, ?, ?)",
                "李四", "lisi@example.com", 19);

        System.out.println("已插入测试数据");
        System.out.println("=====================================");
    }
}
